﻿using FRMS.BUS;
using FRMS.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System;

namespace FRMS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    //[Authorize]
    public class DeviceNetWorkController : ControllerBase
    {
        public DeviceNetWorkController()
        {

        }

        /// <summary>
        /// 设备重启
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("Reboot")]
        public ResponseMessage Reboot()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NetBUS netBUS = new NetBUS();
                netBUS.RunCommand("reboot");
                message.errcode = 0;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "设备重启异常";
            }

            return message;
        }

        /// <summary>
        /// 查询网络信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryNetInfo")]
        public ResponseMessage QueryNetInfo()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NetBUS netBUS = new NetBUS();
                List<NetInfo> NetInfos = netBUS.GetNetInfo();
                message.data = NetInfos;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询网络信息异常";
            }

            return message;
        }

        /// <summary>
        /// 修改网络信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateNetInfo")]
        public ResponseMessage UpdateNetInfo(List<NetInfo> netInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NetBUS netBUS = new NetBUS();
                bool result = netBUS.UpdateNetInfo(netInfo);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "修改失败";
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "修改网络IP异常";
            }

            return message;
        }

        /// <summary>
        /// 读取ntp服务器地址
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("GetNtpIp")]
        public ResponseMessage GetNtpIp()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NetBUS netBUS = new NetBUS();
                string result = netBUS.GetNtpIp();

                message.errcode = 0;
                message.data = result;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询Ntp服务器地址异常";
            }

            return message;
        }

        /// <summary>
        /// 修改ntp服务器地址
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateNtpIp")]
        public ResponseMessage UpdateNtpIp(string ip)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NetBUS netBUS = new NetBUS();
                bool result = netBUS.SetNtp(ip);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "修改Ntp服务器失败";
                }


            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询Ntp服务器地址异常";
            }

            return message;
        }

        /// <summary>
        /// 手动设置时间
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("CalibrationTime")]
        public ResponseMessage CalibrationTime(string time)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NetBUS netBUS = new NetBUS();
                bool result = netBUS.CalibrationTime(time);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "手动设置时间失败";
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "手动设置时间异常";
            }

            return message;
        }
    }

}
